﻿using CQIE.LabAssist.DataAccesss;
using CQIE.LabAssist.Models;
using Microsoft.AspNetCore.Mvc;

namespace CQIE.LabAssist.Controllers
{
    /// <summary>
    /// 楼层控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FloorController : Controller
    {
        private readonly IFloorService _floorService;


        public FloorController(_DbContext _DbContext, IFloorService floorService)
        {
            _floorService = floorService;
        }

        #region  根据楼栋id获取所有楼层
        /// <summary>
        ///  根据楼栋id获取所有楼层
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult getAllFloor(int buildingId)
        {
            return new JsonResult(_floorService.getAllFloor(buildingId));
        }
        #endregion

        #region 新增楼层

        public record floor
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public int buildingId { get; set; }
        }
        /// <summary>
        /// 新增楼层
        /// </summary>
        /// <param name="floor"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult addFloor([FromBody] floor floor)
        {
            Floor f = new Floor();
            f.Name = floor.Name;
            f.buildingId = floor.buildingId;
            if (_floorService.addFloor(f))
            {
                return new JsonResult("添加成功");
            }
            else
            {
                return new JsonResult("添加失败");

            }

        }
        #endregion

        #region 删除楼栋

        /// <summary>
        /// 删除楼层
        /// </summary>
        /// <param name="floorId"></param>
        /// <returns></returns>
        [HttpDelete]
        public JsonResult deleteFloor(int floorId)
        {
            if (_floorService.deteltFloor(floorId))
            {
                return new JsonResult("删除成功");
            }
            else
            {
                return new JsonResult("删除失败");

            }

        }
        #endregion

        #region 修改楼层
        /// <summary>
        /// 修改楼层
        /// </summary>
        /// <param name="floor"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult updateFloor(floor floor)
        {
            Floor f = new Floor();
            f.Id = floor.Id;
            f.Name = floor.Name;
            f.buildingId = floor.buildingId;
            if (_floorService.updateFloor(f))
            {
                return new JsonResult("修改成功");
            }
            else
            {
                return new JsonResult("修改失败");
            }
        }
        #endregion
    }
}
